﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.IRepository.SystemSettings.InstituTions;
using SystemCenterSoa.PoModel.SystemSettings.InstituTions;
using SystemCenterSoa.Repository.CommonLib;

namespace SystemCenterSoa.Repository.SystemSettings.InstituTions
{
    public class InterviewRepository : BaseRepository, IInterviewRepository
    {
        public InterviewRepository(SystemCenterDbSoaContext systemCenterDbSoaContext) : base(systemCenterDbSoaContext)
        {


        }

        public List<DepartmentOfManagementPo> BindDep()
        {
            return _systemCenterDbSoaContext.DepartmentOfManagementPo.ToList();
        }

        public InterviewManagementPo GetInterMentById(int id)
        {
            return _systemCenterDbSoaContext.InterviewManagementPo.FirstOrDefault(c => c.inter_ment_id == id);
        }

        public bool InterMentAdd(InterviewManagementPo view)
        {
            _systemCenterDbSoaContext.InterviewManagementPo.Add(view);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        public bool InterMentDelete(int id)
        {
            var list = _systemCenterDbSoaContext.InterviewManagementPo.Find(id);
            _systemCenterDbSoaContext.InterviewManagementPo.Remove(list);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        public bool InterMentUpdate(InterviewManagementPo view)
        {
            _systemCenterDbSoaContext.Entry(view).State = EntityState.Modified;
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        public PagerResult<InterviewManagementPo> SearchPage(SearchModel searchModel)
        {
            var query = _systemCenterDbSoaContext.InterviewManagementPo.AsQueryable();
            //判断有没有搜索条件
            if (searchModel.SearchItem != null && searchModel.SearchItem.Count > 0)
            {
                //把搜索条件拿出来
                Dictionary<string, string> tempDi = searchModel.SearchItem;
                //tempDi["user_name"]
                //搜索字段为user_name  
                string Name = "name";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(Name) && !string.IsNullOrEmpty(tempDi[Name]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    query = query.Where(c => c.inter_ment_name.Contains(tempDi[Name]));
                }
                string depid = "depid";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(depid) && !string.IsNullOrEmpty(tempDi[depid]))
                {
                    int aaa = 0;
                    if (int.TryParse(tempDi[depid], out aaa))
                    {
                        //类似于like查询, 判断userName是否包含查询条件中的条件值
                        query = query.Where(c => c.dep_id.Equals(tempDi[depid]));
                    }
                }
                string resultid = "resultid";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(resultid) && !string.IsNullOrEmpty(tempDi[resultid]))
                {
                    int aaa = 0;
                    if (int.TryParse(tempDi[resultid], out aaa))
                    {
                        //类似于like查询, 判断userName是否包含查询条件中的条件值
                        query = query.Where(c => c.inter_ment_result.Equals(tempDi[resultid]));
                    }
                }
            }
            return BasePageSearch(query, searchModel);
        }
    }
}
